<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_category_info".
 *
 * @property int $id
 * @property int $cat_id
 * @property string $cat_name
 * @property string $albums_image
 * @property int $score 1-5
 * @property int $buyer
 * @property string $opening_hours 开园时间
 * @property string $scenic_address 景点地址
 * @property string $scenic_introduce 景点介绍
 * @property string $label
 * @property int $floor_price
 * @property string $update_time
 */
class CategoryInfo extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_category_info';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['cat_id'], 'required','message' => '分类id，不能为空'],
            [['cat_id', 'buyer', 'floor_price'], 'integer','message' => '参数格式不正确'],
            [['scenic_introduce','score','bot_content','video_url','albums_image','cat_name'], 'string'],
            [['update_time','other_info','latitude','status','end_time'], 'safe'],
            [['cat_name'], 'string', 'max' => 64,'message' => '名称最大64个字符'],
            [['albums_image'], 'string', 'max' => 1024,'message' => '轮播图最大1024个字符'],
            [['opening_hours', 'scenic_address'], 'string', 'max' => 255,'message' => '景区地址最大255个字符'],
            [['label'], 'string', 'max' => 512],


        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cat_id' => 'Cat ID',
            'cat_name' => 'Cat Name',
            'albums_image' => 'Albums Image',
            'score' => 'Score',
            'buyer' => 'Buyer',
            'video_url' => 'video_url',
            'opening_hours' => 'Opening Hours',
            'scenic_address' => 'Scenic Address',
            'scenic_introduce' => 'Scenic Introduce',
            'label' => 'Label',
            'status' => 'status',
            'floor_price' => 'Floor Price',
            'update_time' => 'Update Time',
            'end_time' => 'end Time',

        ];
    }

    public function getInfo($data)
    {
        if (isset($data['cat_id']) and !empty($data['cat_id'])){
            $info = self::find()->where('cat_id = :cid',[':cid' => $data['cat_id']])->asArray()->one();
            return $info;
        }
        return [];
    }

    public function setInfo($data)
    {
        $paramsData = [
            'CategoryInfo' => [
                'cat_id' => $data['cat_id'],
                'cat_name' => $data['cat_name'],
                'albums_image' => $data['albums_image'],
                'score' => $data['score'],
                'buyer' => $data['buyer'],
                'video_url' => $data['video_url'],
                'opening_hours' => $data['opening_hours'],
                'scenic_address' => $data['scenic_address'],
                'scenic_introduce' => $data['scenic_introduce'],
                'label' => $data['label'],
                'bot_content' => $data['bot_content'],
                'other_info' => $data['other_info'],
                'latitude' => $data['latitude'],
                'status' => $data['status'],
                'floor_price' => $data['floor_price'],
                'end_time' => strtotime($data['end_time']),
                'update_time' => date('Y-m-d H:i:s'),
            ]
        ];

        if ($this->load($paramsData) and $this->validate()){
            $model = self::find()->where('cat_id = :cid',['cid' => $data['cat_id']])->one();
            if (!is_null($model)){
                $model->cat_id = $data['cat_id'];
                $model->cat_name = $data['cat_name'];
                $model->albums_image = $data['albums_image'];
                $model->score = $data['score'];
                $model->buyer = $data['buyer'];
                $model->video_url = $data['video_url'];
                $model->opening_hours = $data['opening_hours'];
                $model->scenic_address = $data['scenic_address'];
                $model->scenic_introduce = $data['scenic_introduce'];
                $model->label = $data['label'];
                $model->floor_price = $data['floor_price'];
                $model->bot_content = isset($data['bot_content'])?$data['bot_content']:'';
                $model->latitude = isset($data['latitude'])?$data['latitude']:'';
                $model->status = isset($data['status'])?$data['status']:'1';
                $model->other_info = isset($data['other_info'])?$data['other_info']:'';
                $model->end_time = strtotime($data['end_time']);
                $model->update_time = date('Y-m-d H:i:s');
                if ($model->save(0)){
                    return true;
                }
            }else{
                if ($this->save(0)){
                    return true;
                }
            }
        }
        return false;
        
        
    }
}
